package com.originskyseed.platform.job.jobhandler.channel;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.job.biz.model.ReturnT;
import com.originskyseed.platform.core.job.handler.IJobHandler;
import com.originskyseed.platform.core.job.handler.annotation.JobHandler;
import com.originskyseed.platform.core.job.log.JobLogger;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.user.model.PlayerRelateChannelLog;
import com.originskyseed.platform.user.model.PlayerRelateRecharge;
import com.originskyseed.platform.user.service.IPlayerRelateChannelLogService;
import com.originskyseed.platform.user.service.IPlayerService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;

/**
 * 渠道更改
 */
@Component
@JobHandler(value = "playerChangeChannelHandler")
public class PlayerChangeChannelHandler extends IJobHandler {

    @DubboReference
    private IPlayerRelateChannelLogService playerRelateChannelLogService;
    @Override
    public ReturnT<String> execute(String tenantCode) throws Exception {
        try {
            if (StringUtils.isNotEmpty(tenantCode)) {
                TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
            }
            //查询新增的玩家信息
            int pageSize=1;
            int pageNo=200;
            HashMap params = new HashMap<>();
            //查询历史玩家新的充值信息
            params.put("pageSize", pageSize);
            params.put("pageNo", pageNo);
            params.put("isUpdate",0);
            BasePage<PlayerRelateChannelLog> rechargePage = new BasePage<PlayerRelateChannelLog>(pageNo, pageSize);
            rechargePage = this.playerRelateChannelLogService.selectListByPageByTenantCode(tenantCode,params,rechargePage);
            if (rechargePage != null && rechargePage.getTotalPage() != null && rechargePage.getTotalPage() > 0) {
                List<PlayerRelateChannelLog> results = rechargePage.getResults();
                if (results != null && results.size() > 0) {
                    playerRelateChannelLogService.updateByPrimaryKeySelectiveByTenantCode(tenantCode,results);
                }
                int count = rechargePage.getTotalPage();

                if (count > 1) {
                    for (int i = 2; i <= count; i++) {
                        pageNo = i;
                        params.put("pageNo", pageNo);
                        rechargePage = new BasePage<PlayerRelateChannelLog>(pageNo, pageSize);
                        rechargePage = this.playerRelateChannelLogService.selectListByPageByTenantCode(tenantCode,params,rechargePage);
                        results = rechargePage.getResults();
                        if (results != null && results.size() > 0) {
                            playerRelateChannelLogService.updateByPrimaryKeySelectiveByTenantCode(tenantCode,results);
                        }
                    }
                }
            }
        } catch (Exception e) {
            JobLogger.log("渠道更改：" + getExceptionDetail(e));
            return FAIL;
        }
        return SUCCESS;
    }


    /**
     * 获取异常详细信息，知道出了什么错，错在哪个类的第几行 .
     *
     * @param ex
     * @return
     */
    public static String getExceptionDetail(Exception ex) {
        String ret = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream pout = new PrintStream(out);
        try {
            ex.printStackTrace(pout);
            ret = new String(out.toByteArray());
        } catch (Exception e) {
        } finally {
            try {
                pout.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }
}
